<?php
$fileName = "./chat.txt";
$maxCount = 6;

function secure_string($str)
{
  return strip_tags($str);
}

if(isSet($_GET['nick']) && isSet($_GET['dane'])){
  $nick = $_GET['nick'];
  $dane = $_GET['dane'];
  if($nick == "" || $dane == ""){
    die("error:Niepoprawne dane.");
  }
  if(($arr = @file($fileName)) === false){
    die("error:Bd serwera. Dane chatu s niedostpne.");
  }
  //$dateTxt = date("Y-m-d G:i:s");
  $dateTxt = date("G:i:s");
  $str = "$dateTxt | $nick | $dane\r\n";
  $str = secure_string($str);
  $count = array_push($arr, $str);
  if ($count > $maxCount) array_shift($arr);
  if(!@file_put_contents($fileName, $arr)){
    die("error:Bd serwera. Dane nie zostay zapisane.");
  }
}
else if(isSet($_GET['rows'])){
  $rows = $_GET['rows'];
  if(!is_numeric($rows)){
    die("error:Niepoprawne dane.");
  }
  if(($arr = @file($fileName)) === false){
    die("error:Bd serwera. Dane chatu s niedostpne.");
  }
  $count = count($arr);
  if($count <= $rows){
    for($i = 0; $i < $count; $i++){
      echo "<div>{$arr[$i]}</div>";
    }
  }
  else{
    for($i = count($arr) - $rows; $i < $count; $i++){
      echo "<div>{$arr[$i]}</div>";
    }
  }
}
else{
  echo "error:Niepoprawne dane.";
}
?>